package com.touka.controller;

import com.touka.common.Result;
import com.touka.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/common")
public class UploadController {
  @Autowired
  private AliOSSUtils aliOSSUtils;

  @PostMapping("/upload")
  @CrossOrigin(origins = "*", maxAge = 3600) // 允许改接口跨域
  public Result<String> upload(MultipartFile file) throws Exception {
    String url = aliOSSUtils.upload(file);
//    // 获取原始文件名
//    String filename = file.getOriginalFilename();
//
//    // 构造唯一的文件名 uuid(通用唯一识别码)
//    int index = filename.lastIndexOf(".");
//    String extname = filename.substring(index); // 获取文件后缀名
//    String newFileName = UUID.randomUUID().toString() + extname; // 唯一文件名
//    log.info("新的文件名：{}", newFileName);
//
//    // 将上传的文件存储在服务器的磁盘目录中 D:\\uploadFiles
//    file.transferTo(new File("D:\\sakura\\java\\javaxiangmu\\reggie-take-out\\src\\main\\resources\\common\\download\\" + newFileName));

    return Result.success(url);
  }
}
